import { ElMessage } from 'element-plus'

var wsHost = 'localhost'

// function errMessage() {
//   ElMessage.error("连接视频服务失败");
// }
//打开实时报警弹框
export function layoutAlarmList(obj) {
    return new Promise((resolve, reject) => {
        var list = obj.list
        var ws = new WebSocket('ws://' + wsHost + ':8888/PopWnd/Alarm')
        ws.onerror = function () {
            // errMessage()
            ws = null
            reject()
        }
        ws.onopen = function () {
            ws.send(JSON.stringify(list))
            ws.close()
            ws = null
            resolve()
        }
    })
}

//打开实时视频
export function layoutRealList(obj) {
    console.log(obj)
    return new Promise((resolve, reject) => {
        commandVideoTitle(obj.title || '')
        var list = obj.list
        var ws = new WebSocket('ws://' + wsHost + ':8888/PopWnd/OpenLive')
        ws.onerror = function () {
            // errMessage()
            ws = null
            reject()
        }
        ws.onopen = function () {
            ws.send(JSON.stringify(list))
            ws.close()
            ws = null
            resolve()
        }
    })
}
//设置视频组件title
export async function commandVideoTitle(title) {
    var ws = new WebSocket('ws://' + wsHost + ':8888/PopWnd/Command')
    ws.onerror = function () {
        // errMessage()
        ws = null
    }
    ws.onopen = function () {
        ws.send(
            JSON.stringify({
                command: 5,
                param: {
                    Caption: title
                }
            })
        )
        ws.close()
        ws = null
    }
}
//打开历史视频
export function layoutHisList(obj) {
    commandVideoTitle(obj.title || '')
    var list = obj.list
    if (obj.type !== 1) {
        list.forEach(function (item) {
            item.begin_time = ''
            item.end_time = ''
        })
    } else {
        list.forEach(function (item) {
            item.play_time = item.begin_time
        })
    }
    var ws = new WebSocket('ws://' + wsHost + ':8888/PopWnd/OpenPlayback')
    ws.onerror = function () {
        ws = null
    }
    ws.onopen = function () {
        ws.send(JSON.stringify(list))
        ws.close()
        ws = null
    }
}
/**
 * 视频窗口操作
 * @param {*} n
 * @returns
 */
export function commandVideo(n) {
    return new Promise((resolve, reject) => {
        var ws = new WebSocket('ws://' + wsHost + ':8888/PopWnd/Command')
        ws.onerror = function () {
            // errMessage()
            ws = null
            reject()
        }
        ws.onopen = function () {
            ws.send(
                JSON.stringify({
                    command: n
                })
            )
            ws.close()
            ws = null
            resolve()
        }
    })
}
export function openLiveVideo(obj) {
    return new Promise((resolve, reject) => {
        var ws = new WebSocket('ws://' + wsHost + ':8888/Exec/LiveView')
        ws.onerror = function () {
            // errMessage()
            ws = null
            reject()
        }
        ws.onopen = function () {
            ws.send(JSON.stringify(obj))
            ws.close()
            ws = null
            resolve()
        }
    })
}
